//
//  CZPushModalViewController.swift
//  GZWeibo007
//
//  Created by 颜祥 on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import SVProgressHUD
import SnapKit

class CZPushForwardViewController: UIViewController
{
    
    var status: CZStatus? {
        didSet {
            print("\(status)")
            
            if status == nil {
                return
            }
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        let animation = CABasicAnimation(keyPath: "transform.rotation.y")
        animation.fromValue = (-M_PI)
        animation.removedOnCompletion = false
        animation.fillMode = kCAFillModeForwards;
        animation.duration = 1.0;
        self.view.layer.addAnimation(animation, forKey: nil);
    }
    
    func respont(statu: String) {
        
       CZNetworkTool.sharedInstance.creatForward(statu, id: (status?.id)!) { (error) -> () in
        
        if error != nil {
            
           SVProgressHUD.showInfoWithStatus("网络出错了亲")
            return
        }
        
        SVProgressHUD.showInfoWithStatus("转发成功", maskType: SVProgressHUDMaskType.Black)
        }
    }
    
    override func viewDidLoad()
    {
        // 添加导航条按钮
        setupNavigationBarButton()
        
        // textView光标移动到左上角
        automaticallyAdjustsScrollViewInsets = false
        // 添加输入框
        view.addSubview(writeTextField)
        // 添加约束
        addWriteTextFieldConstraint()
        
        writeTextField.becomeFirstResponder()
  
    }
    
    // 添加输入框约束
    private func addWriteTextFieldConstraint()
    {
//        writeTextField.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(self.snp_topLayoutGuideTop).offset(80)
//            make.leading.equalTo(self.view.snp_leading).offset(20)
//            make.trailing.equalTo(self.view.snp_trailing).offset(-20)
//            make.height.equalTo(self.view.snp_height).multipliedBy(0.1)
//        }
        writeTextField.translatesAutoresizingMaskIntoConstraints = false
        
        view.addConstraint(NSLayoutConstraint(item: writeTextField, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 74))
        view.addConstraint(NSLayoutConstraint(item: writeTextField, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 8))
        view.addConstraint(NSLayoutConstraint(item: writeTextField, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -8))
        view.addConstraint(NSLayoutConstraint(item: writeTextField, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
    }
    
    // 添加导航条按钮
    private func setupNavigationBarButton()
    {
        // 添加导航条按钮
        // 取消
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: "cancel")
        
        // 发送
        sendButton.frame = CGRect(x: 0, y: 0, width: 45, height: 25)
        sendButton.layer.masksToBounds = true
        sendButton.layer.cornerRadius = 5
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: sendButton)
        
    }
    
    // MARK: - 按钮点击事件
    // 点击取消
    func cancel()
    {
        dismissViewControllerAnimated(false, completion: nil)
    }
    
    // 点击发送
    func send()
    {
        // 获得writeTextField内容
         let text = writeTextField.text!
        
        // 跳转到指定界面,将text回传
        print("发送的内容: \(text)")
        respont(text)
        cancel()
    }

    // MARK: - 懒加载输入框
    private lazy var writeTextField: UITextView =
    {
        let writeTextField = UITextView()
        writeTextField.backgroundColor = UIColor.redColor()
        writeTextField.font = UIFont.systemFontOfSize(16)
        //writeTextField.placeHodler.text = "说说分享心得..."
        //writeTextField.placeHodler.backgroundColor = UIColor.grayColor()
        writeTextField.layer.cornerRadius = 8
        writeTextField.layer.masksToBounds = true
        
        // 监听writeTextField文本内容的改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "writeTextFieldValueChange", name: UITextViewTextDidChangeNotification, object: writeTextField)
        return writeTextField
    }()
    
    // 发送按钮
    private lazy var sendButton: UIButton =
    {
        let sendButton = UIButton(type: UIButtonType.Custom)
        //sendButton.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        sendButton.backgroundColor = UIColor.orangeColor()
        sendButton.setTitle("发送", forState: UIControlState.Normal)
        sendButton.titleLabel?.font = UIFont.systemFontOfSize(13)
        //sendButton.enabled = false
        sendButton.addTarget(self, action: "send", forControlEvents: UIControlEvents.TouchUpInside)
        
        return sendButton

    }()
    
    var label: UILabel = {
        let label = UILabel()
        label.text = "正在输入中..."
        label .sizeToFit()
        return label
    }()
    
    // 监听文本输入框文本的变化
    func writeTextFieldValueChange()
    {
        
        self.navigationItem.titleView = label
        let animation = CABasicAnimation(keyPath: "transform.scale")
        animation.fromValue = (0.8)
        animation.toValue = (1.0)
        animation.duration = 1.0
        animation.repeatCount = MAXFLOAT
        label.layer .addAnimation(animation, forKey: nil)
        //sendButton.enabled = writeTextField.text?.characters.count == 0 ? false : true
        

    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
    }
}
